//
//  NSString+ZCExtension.m
//  ZCBaiSi
//
//  Created by zc on 16/7/20.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "NSString+ZCExtension.h"

@implementation NSString (ZCExtension)
- (unsigned long long)fileSize{
    //总大小
    unsigned long long size = 0;
    //文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    //文件属性
    NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
    if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) {
      //文件夹
        //获取文件夹的大小 == 获得文件夹中所有文件的总大小
        //[mgr contentsOfDirectoryAtPath:dirpath error:nil];
        //是否为文件夹
//        BOOL isDirectory = NO;
//        //路径是否存在
//        BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
        NSArray *subpaths = [mgr subpathsAtPath:self];
        for (NSString *subpath in subpaths) {
            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
            //获得文件夹属性
            NSDictionary *atts = [mgr attributesOfItemAtPath:fullSubpath error:nil];
            //累加文件大小
            size += atts.fileSize;
        }
    }else{
        //文件
        size = attrs.fileSize;
    }
   
   // ZCLog(@"%zd",size);
    return size;
}
@end
